#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main()
{
    int fd = open("T_log.txt", O_CREAT | O_RDWR | O_APPEND, 0666);
    if(-1 == fd)
    {
        perror("open fail!");
        _exit(1);
    }
    close(1);
    dup2(fd, 1);
    while(1)
    {
        char buffer[1024];
        ssize_t s = read(0, buffer, sizeof(buffer) - 1);
        if (s < 0) {
            perror("read");
            break;
        }
        printf("%s", buffer);
        fflush(stdout);
    }
    close(fd);
    return 0;
}
